package com.sgl.gk.controller;

import com.sgl.common.annotation.Log;
import com.sgl.common.core.controller.BaseController;
import com.sgl.common.core.domain.AjaxResult;
import com.sgl.common.core.page.TableDataInfo;
import com.sgl.common.enums.BusinessType;
import com.sgl.common.utils.ServletUtils;
import com.sgl.common.utils.StringUtils;
import com.sgl.gk.domain.GkSignalCommunication;
import com.sgl.gk.service.GkSignalCommunicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author:xec
 * @date:2022/10/31 18:50
 *
 * 通信管理表的controller
 **/
@RestController
@RequestMapping("/gk/signalcommunication")
public class GkSignalCommunicationController extends BaseController {

    @Autowired
    GkSignalCommunicationService gkSignalCommunicationService;

    /**
     * 按条件分页查询产线信息
     * @param gkSignalCommunication 查询条件
     * @return
     */
    @ResponseBody
    @GetMapping("/list")
    public TableDataInfo list(GkSignalCommunication gkSignalCommunication){
        if(ServletUtils.getParameter("pageSize")!=null&&ServletUtils.getParameter("pageNum")!=null){
            startPage();
        }
        List<GkSignalCommunication> list = gkSignalCommunicationService.list(gkSignalCommunication);
        return getDataTable(list);
    }

    /**
     * 添加通信信息
     * @param gkSignalCommunication
     * @return
     */
    @Log(title = "通信",businessType = BusinessType.INSERT)
    @ResponseBody
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GkSignalCommunication gkSignalCommunication){
        if (gkSignalCommunication.getName()==null||gkSignalCommunication.getName()==""){
            return AjaxResult.error("通信名称不能为空");
        }
        if (gkSignalCommunication.getCommunicationProtocol()==null||gkSignalCommunication.getCommunicationProtocol()==""){
            return AjaxResult.error("通信协议不能为空");
        }
        if (StringUtils.isEmpty(gkSignalCommunication.getIp())||StringUtils.isEmpty(gkSignalCommunication.getPort())||StringUtils.length(gkSignalCommunication.getPort())>10||StringUtils.length(gkSignalCommunication.getIp())>25) {
            return AjaxResult.error("ip和端口号不能为空且ip不能超过25个字符端口不能超过10个字符");
        }
        gkSignalCommunication.setName(gkSignalCommunication.getName().trim());
        gkSignalCommunication.setCommunicationProtocol(gkSignalCommunication.getCommunicationProtocol().trim());
        return gkSignalCommunicationService.add(gkSignalCommunication);
    }

    /**
     * 修改通信信息
     * @param gkSignalCommunication
     * @return
     */
    @Log(title = "通信",businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GkSignalCommunication gkSignalCommunication){
        if (gkSignalCommunication.getName()==null||gkSignalCommunication.getName()==""){
            return AjaxResult.error("通信名称不能为空");
        }
        if (gkSignalCommunication.getCommunicationProtocol()==null||gkSignalCommunication.getCommunicationProtocol()==""){
            return AjaxResult.error("通信协议不能为空");
        }
        if (StringUtils.isEmpty(gkSignalCommunication.getIp())||StringUtils.isEmpty(gkSignalCommunication.getPort())||StringUtils.length(gkSignalCommunication.getPort())>10||StringUtils.length(gkSignalCommunication.getIp())>25) {
            return AjaxResult.error("ip和端口号不能为空且ip不能超过25个字符端口不能超过10个字符");
        }
        gkSignalCommunication.setName(gkSignalCommunication.getName().trim());
        gkSignalCommunication.setCommunicationProtocol(gkSignalCommunication.getCommunicationProtocol().trim());
        return gkSignalCommunicationService.edit(gkSignalCommunication);
    }

    /**
     * 删除通信信息
     * @param ids
     * @return
     */
    @Log(title = "通信",businessType = BusinessType.DELETE)
    @ResponseBody
    @DeleteMapping("/{ids}")
    public AjaxResult removeByIds(@PathVariable("ids") Long[] ids){
        return gkSignalCommunicationService.removeByIds(ids);
    }
}
